package cn.sirun.com.fca.account.presenter.impl;


import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.constant.AccountActionConst;
import cn.sirun.com.fca.account.loader.LoginLoader;
import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.account.model.login.AuthenticateRequest;
import cn.sirun.com.fca.account.model.login.AuthenticateResponse;
import cn.sirun.com.fca.account.model.login.ChallengeRequest;
import cn.sirun.com.fca.account.model.login.ChallengeResponse;
import cn.sirun.com.fca.account.model.login.LoginRequest;
import cn.sirun.com.fca.account.model.login.LoginResponse;
import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.account.model.login.PersonalInfoRequest;
import cn.sirun.com.fca.account.model.login.VehicleRespons;
import cn.sirun.com.fca.account.observer.CommonExceptionObserver;
import cn.sirun.com.fca.account.presenter.AccountFlowPresenter;
import cn.sirun.com.fca.account.presenter.ILoginPresenter;
import cn.sirun.com.fca.account.service.LoginManager;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.BehaviorSubject;

public class LoginPresenter extends AccountFlowPresenter<LoginLoader> implements ILoginPresenter {
    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected LoginLoader createLoader() {
        return new LoginLoader();
    }

    @Override
    public void challenge(final ChallengeRequest request) {
        getLoader().challenge(request)
                .takeUntil(mBehaviorSubject)
                .doOnNext(new Consumer<BaseResponse<ChallengeResponse>>() {
                    @Override
                    public void accept(BaseResponse<ChallengeResponse> response) throws Exception {
                        if (response != null && StatusCode.SUCCESS == response.getCode()) {
                            LoginManager.getInstance().updatePhoneInfo(
                                    request.getNation(), request.getPhone()
                            );
                        }
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonExceptionObserver<ChallengeResponse>(this) {
                    @Override
                    public void onNext(BaseResponse<ChallengeResponse> response) {
                        super.onNext(response);
                        onAction(AccountActionConst.Normal.ACTION_LOGIN_CHALLENGE_RESPONSE,
                                response);
                    }
                });
    }

    @Override
    public void passwordLogin(LoginRequest request) {
        getLoader().passwordLogin(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonExceptionObserver<LoginResponse>(this) {
                    @Override
                    public void onNext(BaseResponse<LoginResponse> response) {
                        super.onNext(response);
                        onAction(AccountActionConst.Normal.ACTION_PASSWODR_LOGIN_RESULT, response);
                    }
                });
    }

    @Override
    public void authenticate(final AuthenticateRequest request) {
        getLoader().authenticate(request)
                .takeUntil(mBehaviorSubject)
                .doOnNext(new Consumer<BaseResponse<AuthenticateResponse>>() {
                    @Override
                    public void accept(BaseResponse<AuthenticateResponse> response)
                            throws Exception {
                        if (StatusCode.SUCCESS == response.getCode()) {
                            LoginManager.getInstance().handleLoginResult();
                        }
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonExceptionObserver<AuthenticateResponse>(this) {
                    @Override
                    public void onNext(BaseResponse<AuthenticateResponse> response) {
                        super.onNext(response);
                        onAction(AccountActionConst.Normal.ACTION_LOGIN_AUTHENTICATE_RESPONSE,
                                response);
                    }
                });
    }

    @Override
    public void getVehicle() {
        getLoader().getVehicle()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonExceptionObserver<VehicleRespons>(this) {
                    @Override
                    public void onNext(BaseResponse<VehicleRespons> response) {
                        super.onNext(response);
                        onAction(AccountActionConst.Normal.ACTION_GET_VEHICLE_RESULT, response);
                    }
                });
    }

    @Override
    public void getPersonalInfo(PersonalInfoRequest request) {
        getLoader().getPersonalInfo(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<PersonalInfo>>() {

                    @Override
                    public void onNext(BaseResponse<PersonalInfo> personalInfoResponse) {
                        super.onNext(personalInfoResponse);
                        getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_GET_PERSONAL_INFO_RESPONSE,
                                        personalInfoResponse);
                    }
                });
    }

    @Override
    public void getCarInfo(String din) {
        getLoader().getCarInfo(din)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<CarInfoDetailResponse>>() {

                    @Override
                    public void onNext(BaseResponse<CarInfoDetailResponse> carInfoResponse) {
                        super.onNext(carInfoResponse);
                        getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_GET_CAR_INFO_RESPONSE,
                                        carInfoResponse);
                    }
                });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }


}
